<?php

error_reporting(E_ALL);

require_once(dirname(__FILE__).'/WriteExcel/WriteExcel.php');
require_once(dirname(__FILE__).'/WriteWord/WriteWord.php');
require_once(dirname(__FILE__).'/personalInfo.php');
$person_id=$browser_info->id;

function exportToTable($cont,$export_type,$end=false)
{
	static $write_time=0;//既是写入次数也是行指针
	static $cont_all=array();
	$base_dir=dirname(__FILE__).'/searchResult/';
	if($end){
		
		$columns=array();
		if(!empty($_POST['exportItems'])){
			$columns=explode('__',$_POST['exportItems']);
		}
		else{
			return;
		}
		global $person_id;
		$filename=hash('adler32',$person_id);
		
		if(strpbrk($export_type,"Ee")){
			$export_path=$base_dir.$filename.".xls";
			$has_author=false;
			if(!empty($_POST['merge_all_authors'])){//合并作者 
				//echo "Merging".EOL;
				foreach($columns as $key=>$col)
				{
					if(!empty($col) && strstr($col,'作者')){
						if($has_author){unset($columns[$key]);}
						else{$columns[$key]='作者';$has_author=true;}
					}
				}
			}
			saveToExcel($cont_all,'B',$columns,$export_path,2);
			//echo "Row:".$write_time.EOL;
		}
		else{
			$export_path=$base_dir.$filename.".doc";
			$del_array=array();
			$col_del=array();//$col_del是列和相对应的分隔符的映射
			if(!empty($_POST['exportDels'])){//如果分隔符没出问题
				$del_array=explode("__",$_POST['exportDels']);
				for($i=0;$i<count($columns);++$i)
				{
					$col_del[$columns[$i]]=$del_array[$i];
				}
			}
			else{
				for($i=0;$i<count($columns);++$i)
				{
					$col_del[$columns[$i]]=".";//默认的分隔符
				}
			}
			
			saveToWord($cont_all,$col_del,$export_path);
		}
		
	}
	else{
		$cont_all[]=$cont;
		$write_time+=1;	
	}
}
?>